package net.nandleon.AgenceVoyage.client.accueil;

import java.util.Date;
import java.util.List;
import java.util.Map;

import net.nandleon.AgenceVoyage.client.accueil.activiteCategory.ActivitesCategory;
import net.nandleon.AgenceVoyage.client.accueil.administration.Administration;
import net.nandleon.AgenceVoyage.client.accueil.dataPanel.DataPanel;
import net.nandleon.AgenceVoyage.client.accueil.offresMomentCellule.OffresMomentCellule;
import net.nandleon.AgenceVoyage.client.accueil.promotionCellule.PromotionCellule;
import net.nandleon.AgenceVoyage.client.event.EventBus;
import net.nandleon.AgenceVoyage.client.event.dataEvent.DataEvent;
import net.nandleon.AgenceVoyage.client.event.dataEvent.DataEventHandler;
import net.nandleon.AgenceVoyage.client.event.loginEvent.LoginEvent;
import net.nandleon.AgenceVoyage.client.event.loginEvent.LoginEventHandler;
import net.nandleon.AgenceVoyage.client.login.Login;
import net.nandleon.AgenceVoyage.client.panier.PanierView;
import net.nandleon.AgenceVoyage.client.ressources.Ressources;
import net.nandleon.AgenceVoyage.client.services.LoginService;
import net.nandleon.AgenceVoyage.client.services.LoginServiceAsync;
import net.nandleon.AgenceVoyage.shared.ElementPanier;
import net.nandleon.AgenceVoyage.shared.OffresMoment;
import net.nandleon.AgenceVoyage.shared.Panier;
import net.nandleon.AgenceVoyage.shared.Promotion;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class Accueil extends Composite implements LoginEventHandler,DataEventHandler {

	private static final Binder binder = GWT.create(Binder.class);
	private final LoginServiceAsync loginService = GWT.create(LoginService.class);
	interface Binder extends UiBinder<Widget, Accueil> {
	}

	static {
		Ressources.INSTANCE.uiAccueilStyle().ensureInjected();
	}

	@UiField
	Button buttonActionLogin;

	@UiField
	FocusPanel panierPanel;

	@UiField
	MenuItem itemAccueil;

	@UiField
	MenuItem itemActivites;

	@UiField
	MenuItem itemLogements;

	@UiField
	MenuItem itemTransports;

	@UiField
	MenuItem itemLieux;

	@UiField
	Label labelUtilisateur;

	@UiField
	MenuBar menuBarAccueil;
	
	@UiField
	MenuItem itemAdmin;

	@UiField
	FlowPanel dataPanel;
	
	 @UiField
	 Label labelPanier;
	
	 @UiField
	 Label nbElement;
	 
	 @UiField
	 Button viewPanier;
	 
	 
	 
	 Panier monPanier;
	 
	 
	 
	public Accueil() {
		
		monPanier = new Panier();
		EventBus.bus.addHandler(LoginEvent.TYPE, this);
		EventBus.bus.addHandler(DataEvent.TYPE, this);
		
		initWidget(binder.createAndBindUi(this));

		dataPanel.add(new DataPanel());
		
		Command cmdAccueil = new Command() {
			public void execute() {
				dataPanel.clear();
				dataPanel.add(new DataPanel());
			}
		};
		
		
		Command cmdActivites = new Command() {
			public void execute() {
				dataPanel.clear();
				dataPanel.add(new ActivitesCategory());
				dataPanel.setSize("600px", "250px");
			}
		};

		Command cmdLogements = new Command() {
			public void execute() {
			}
		};

		Command cmdTransports = new Command() {
			public void execute() {
			}
		};

		Command cmdLieux = new Command() {
			public void execute() {
			}
		};

		Command cmdAdmin = new Command() {
			public void execute() {
				dataPanel.clear();
				dataPanel.add(new Administration());
				dataPanel.setSize("600px", "250px");
			}
		};
		
		itemAccueil.setCommand(cmdAccueil);
		itemActivites.setCommand(cmdActivites);
		itemLogements.setCommand(cmdLogements);
		itemLieux.setCommand(cmdLieux);
		itemTransports.setCommand(cmdTransports);
		
		itemAdmin.setCommand(cmdAdmin);
		itemAdmin.setVisible(false);

		
		
	}

	@UiHandler("buttonActionLogin")
	void handleClick(ClickEvent e) {
		Button button = (Button) e.getSource();
		if(button.getText().equals("Connexion")){
			
			button.setText("Deconnexion");
			
			 String sessionID = Cookies.getCookie("sid");
			 if ( sessionID != null ){
		            loginService.verifyLogin(sessionID,
		                    new AsyncCallback<Map>() {
		                        @Override
		                        public void onFailure(Throwable caught) {
		                            Window.alert("Server communication error");
		                        }
	
		                        @Override
		                        public void onSuccess(Map result) {
		                        	if(result != null){
			                            LoginEvent event = new LoginEvent();
			                            event.setContent("Bienvenue "+result.get("nom"));
			                            
			                            if(result.get("nom").toString().equals("root")){
			                            	event.setIsRoot(true);
			                            }
			                            
			                            event.setMonPanier((Panier) result.get("panier"));
			                            EventBus.bus.fireEvent(event);
		                        	}
		                        	else{
		                        		Window.alert("Utilisateur Inconnu");
		                        		Login wpForm = new Login();
		                    			wpForm.center();
		                        	}
	
		                            
		                        }
		                    });
			 }
		    else {
				Login wpForm = new Login();
				wpForm.center();		    	
	
		    }

		}
		else{
			//Deconnexoin
			button.setText("Connexion");
			Cookies.removeCookie("sid");
			LoginEvent event = new LoginEvent();
            event.setContent("Cliquer sur Connexion");
            event.setIsRoot(false);
           
            event.setMonPanier(new Panier());
            EventBus.bus.fireEvent(event);
			
		}
	}

	@Override
	public void onLoginConnect(LoginEvent event) {

		labelUtilisateur.setText(event.getContent());
		itemAdmin.setVisible(event.getIsRoot());

		monPanier=event.getMonPanier();
		nbElement.setText(monPanier.getNbArticles()+" articles");
		
		
	}

	@Override
	public void onMarkerSelected(DataEvent event) {
		
		if(event.getElementPanier() != null){
			monPanier.addElement(event.getElementPanier());
			
		}
		nbElement.setText(monPanier.getNbArticles()+" articles");
		sauvegarderPanier();
		
	}
	
	private void sauvegarderPanier() {
		String sessionID = Cookies.getCookie("sid");

		 if ( sessionID != null ){
			 loginService.backupPanier(sessionID,monPanier,
	                    new AsyncCallback<String>() {
	                    	  @Override
		                        public void onFailure(Throwable caught) {
		                            Window.alert("Server communication error");
		                        }

		                        @Override
		                        public void onSuccess(String result) {
		                        	
		                        }
				 
			 });
		 
		 }
		
	}

	@UiHandler("viewPanier")
	void handleClick2(ClickEvent e) {
	
		dataPanel.clear();
		dataPanel.add(new PanierView(monPanier));
		dataPanel.setSize("600px", "250px");
		

	}

	

}
